package com.mj.model
{
	import com.gamebase.net.rpc.Serializer;
	import com.gamebase.net.rpc.Session;
	import com.mj.vo.SCGetMinLoadRes;
	
	import flash.events.Event;

	public class Balance
	{
		private var _onConnect:Function;
		private var _onClose:Function;
		private var balanceSession:Session;
		
		public function Balance()
		{
		}
		
		public function connectServer(onConnect:Function, onClose:Function=null):void
		{
			_onConnect = onConnect;
			_onClose = onClose;
			balanceSession = new Session();
			balanceSession.connect("whd0810.xicp.net", 21000, ProtocolMap.instance, onBalanceConnect);
			

		}
		
		private function onBalanceConnect(evt:Event):void
		{
			balanceSession.addEventListener(ProtocolMap.BALANCE_CMD_0 + "," + ProtocolMap.SC_GET_MIN_LOAD_RES, onGetBestServer);
			balanceSession.request(ProtocolMap.BALANCE_CMD_0, ProtocolMap.CS_GET_MIN_LOAD_REQ);
		}
		
		private function onGetBestServer($data:SCGetMinLoadRes):void
		{
			
			balanceSession.destroy();
			balanceSession = null;
			Session.instance.connect($data.ip, $data.port, ProtocolMap.instance, _onConnect, _onClose);
		}
		
	}
}